package com.grao.waterschool.human.config;

import com.grao.waterschool.human.utils.BeanToStringUtil;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Type;
import java.net.URI;

public class RestTemplateProxy extends RestTemplate {

    @Override
    public <T> ResponseExtractor<ResponseEntity<T>> responseEntityExtractor(Type responseType) {
        return new NoExceptionRestTemplateResponseEntityExtractor<>(responseType, getMessageConverters());
    }

    @Override
    protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException {
        logger.info("发起请求" + method.toString() + " " + url.toString());
        T t = super.doExecute(url, method, requestCallback, responseExtractor);
        logger.info("请求" + method + " " + url + "返回:" + BeanToStringUtil.stringBean(t));
        return t;
    }
}
